package com.huangguolin.cloud.file.control;

import com.huangguolin.cloud.file.entity.SearchFileEntity;
import com.huangguolin.cloud.file.service.FileService;
import com.huangguolin.cloud.mybatis.entity.PtFileEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PadingRstEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PagingEntity;
import com.huangguolin.cloud.mybatis.ztree.ZtreeEntity;
import com.huangguolin.cloud.spring.control.BaseController;
import com.obs.services.model.CopyObjectRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.ResponseEntity;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/file")
public class FileController extends BaseController {

    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private FileService fileService;



    @RequestMapping("/fileAll")
    public  String fileAllPage(){

        return "file/fileAllPage";
    }

    @RequestMapping("/synchFileData")
    @ResponseBody
    public String synchFileData(){//文件同步
        fileService.synchFileData();
        return successJSON("file.synch.success");
    }

    @RequestMapping("/saveFileRefCopy")
    @ResponseBody
    public String saveFileRefCopy(String fileUuid,String pId){//文件复制
        PtFileEntity ptFileEntity = fileService.saveFileRefCopy(fileUuid,pId);
        return successJSON("file.copy.success");
    }
    //移动文件
    @RequestMapping("/moveFileByBatch")
    @ResponseBody
    public String moveFileByBatch(String fileUuid,String pId){
        PtFileEntity ptFileEntity = fileService.moveFileByBatch(fileUuid,pId);
        return successJSON("file.move.success");
    }


    @RequestMapping("/queryFileListByPage")
    @ResponseBody
    public PadingRstEntity<PtFileEntity> queryFileListByPage(SearchFileEntity search, PagingEntity paging){
        logger.info(search);
        logger.info(paging);

        PadingRstEntity<PtFileEntity> ptFileEntityPadingRstEntity = fileService.queryFileListByPage(search,paging);
        return ptFileEntityPadingRstEntity;

    }


    @RequestMapping("/copy")
    @ResponseBody
    public void copyFile(@RequestParam String sourceBucket, @RequestParam String sourcePath, @RequestParam String targetBucket, @RequestParam String targetPath) {
        CopyObjectRequest request = new CopyObjectRequest(sourceBucket, sourcePath, targetBucket, targetPath);

    }

    @RequestMapping("/queryFileInfoById")
    @ResponseBody
    public PtFileEntity queryFileInfoById(@RequestParam("fileUuid") String fileUuid){
        logger.info("fileUuid" + fileUuid);
        PtFileEntity ptFileEntity =fileService.queryParentFileInfoById(fileUuid);
        return ptFileEntity;
    }
    //文件下载控制层
    @RequestMapping("/downloadFile/{fileUuid}")
    @ResponseBody
    public ResponseEntity<byte[]> downloadFile(@PathVariable("fileUuid") String fileUuid){
        logger.info("fileUuid:" + fileUuid);
        ResponseEntity<byte[]> responseEntity = fileService.downloadFile(fileUuid);
        return responseEntity;
    }
    @RequestMapping("/getPreUrl")
    @ResponseBody
    public String getPreUrl(@RequestParam("fileUuid")String fileUuid){
        logger.info("fileUuid" + fileUuid);
        String linkUrl = fileService.getPreUrl(fileUuid);
        return codeJSON(linkUrl);
    }


    @RequestMapping("/getShareFileLink")
    @ResponseBody
    public String getShareFileLink(String fileUuid,String validityDate){
        logger.info("fileUuid:" + fileUuid + "validityDate:" + validityDate);
        String linkUrl = fileService.getShareFileLink(fileUuid,validityDate);
        return codeJSON(linkUrl);
    }

    @RequestMapping("/getQRCodeShare")
    @ResponseBody
    public ResponseEntity<byte[]> getQRCodeShare(){
        ResponseEntity<byte[]> responseEntity = fileService.getQRCodeShare();
        return responseEntity;
    }

    @RequestMapping("/queryFileZtreeAll")
    @ResponseBody
    public List<ZtreeEntity> queryFileZtreeAll(){
        List<ZtreeEntity> list=fileService.queryFileZtreeall();
        return list;
    }

    //文件上传控制层
    @RequestMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(MultipartHttpServletRequest request) throws IOException {
        MultipartFile multipartFile = request.getFile("fileName");
        String fileName = multipartFile.getOriginalFilename();
        String filePath = request.getParameter("filePath");
        if (!"/".equals(fileName)){
            fileName = filePath + fileName;
        }
        fileService.uploadFile(fileName,multipartFile.getInputStream());
        return successJSON("file.upload.success");
    }

    //文件修改消息交互
    @RequestMapping("/modifyFile")
    @ResponseBody
    public String modifyFile(String fileName,String fileUuid){
        logger.info("fileName: " + fileName + "  fileUuid:" + fileUuid);
        fileService.modifyFile(fileName,fileUuid);
        return successJSON("file.modify.success");
    }

    @RequestMapping("/queryAclZtree")
    @ResponseBody
    public List<ZtreeEntity> queryAclZtree(String fileUuid){
        logger.info(" fileUuid:" + fileUuid);
        List<ZtreeEntity> list = fileService.queryAclZtree(fileUuid);
        return list;
    }

    @RequestMapping("/setObjectAcl")
    @ResponseBody
    public String setObjectAcl(String fileUuid,String aclArray){
        logger.info("aclArray" + aclArray+" fileUuid:" + fileUuid);
        fileService.setObjectAcl(fileUuid,aclArray);
        return successJSON("file.object.acl.success");
    }


    //文件修改消息交互
    @RequestMapping("/deleteFile")
    @ResponseBody
    public String deleteFile(String fileUuid){
        fileService.deleteFile(fileUuid);
        return successJSON("delete.file.success");


    }
}